package com.guoxue.action.front;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.guoxue.domain.TapesShareVo;
import com.guoxue.domain.UserCommentVo;
import com.guoxue.domain.UserVo;
import com.guoxue.service.ITapesShareService;
import com.guoxue.service.IUserCommentService;
import com.guoxue.service.IUserService;
import com.guoxue.util.OSSUtil;
import com.guoxue.util.OtherUtils;

@Controller
@RequestMapping("User")
/**
 * 用户评论处理接口，包括：
 * 1.userComment发表评论
 * 2.删除评论
 * 3.修改评论
 * 4.getComment查询评论列表
 * 
 * @author young
 */
public class CommentAction extends BaseAction{
	@Resource
	private IUserCommentService commentService;
	@Resource
	private IUserService userService;
	@Resource
	private ITapesShareService shareService;
	@Resource
	private OSSUtil oss;
	/**
	 * 评论提交接口
	 */
	@RequestMapping("/upComment")
	@ResponseBody
	public JSON userComment(HttpServletRequest req, HttpServletResponse resp,Integer tapesId,Integer uid,String comment){
		if (isEmpty(comment)){
			return Message(ERROR, "评论为空", null);
		}
		if (tapesId==null){
			return Message(ERROR, "未提交音频id", null);
		}
		if (uid==null){
			return Message(ERROR, "未提交用户id", null);
		}
		UserCommentVo vo = new UserCommentVo();
		vo.setUid(uid);
		vo.setTapesId(tapesId);
		vo.setUserComment(comment);
		vo.setCommitDate(OtherUtils.getCurrentDate());
		commentService.saveComment(vo);
		TapesShareVo share = shareService.getTapesShareByTid(tapesId, 0);
		if (share==null){
			share = new TapesShareVo();
			share.setTapesId(tapesId);
		}else{
			share.setCommentCount(share.getCommentCount()+1);
		}
		shareService.saveTapesShare(share);
		return Message(SUCCESS, "评论成功", null);
	}
	/**
	 * 获取评论
	 * @param req
	 * @param resp
	 * @param tapesId
	 * @param page
	 * @param count
	 * @return
	 */
	@RequestMapping("getTapesComment")
	@ResponseBody
	public JSON getComment(HttpServletRequest req, HttpServletResponse resp,Integer tapesId,Integer start, Integer total, String date){
		if (start==null){
			return Message(ERROR, "未提交开始id", null);
		}
		if (total==null){
			return Message(ERROR, "未指定查询个数", null);
		}
		if (tapesId==null){
			return Message(ERROR, "未提交TapesId", null);
		}
		List<UserCommentVo> userComment = new ArrayList<UserCommentVo>();
		if (date.equals("0")){
			userComment = commentService.getCommentByOrder(start,total,tapesId);
		} else {
			userComment =  commentService.getCommentByOrderAndDate(start,total,tapesId,date);
		}
		if (userComment==null||userComment.isEmpty()){
			return Message(ERROR, "还没有评论哦！", "");
		}
		JSONObject objs = new JSONObject();
		JSONArray array = new JSONArray();
		if (userComment.size()>0){
			for(UserCommentVo u :userComment){
				JSONObject obj = new JSONObject();
				UserVo user = userService.getUserByKey(u.getUid());
				if (user!=null){
					obj.put("commentUser", user.getNickName());
					obj.put("commentUserUrl", isEmpty(user.getImgUrl())?null:oss.getUrl(bucket, user.getImgUrl(),  new Date(new Date().getTime() + 3600 * 1000 * 12)));
					obj.put("comment", u.getUserComment());
					obj.put("commentDate", u.getCommitDate());
				}
				array.add(obj);
			}
			objs.put("comments", array);
		}
		objs.put("time", OtherUtils.getCurrentDate());
		return Message(SUCCESS, "获取评论成功", objs);
	}
}
